package com.example.goods.service.impl;

import com.example.common.api.ResultCode;
import com.example.common.api.ResultUtil;
import com.example.goods.mapper.GoodsClassifyMapper;
import com.example.goods.model.dto.GoodsClassifyDTO;
import com.example.goods.model.po.GoodsClassifyPO;
import com.example.goods.model.vo.GoodsClassifyVO;
import com.example.goods.service.IGoodsClassifyService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class GoodsClassifyService implements IGoodsClassifyService {

    @Resource
    private GoodsClassifyMapper goodsClassifyMapper;

    @Override
    public ResultUtil createGoodsClassify(GoodsClassifyVO goodsClassifyVO) {
        log.info("create goods classify begin, receive params: {}", goodsClassifyVO.toString());

        GoodsClassifyPO goodsClassifyPO = new GoodsClassifyPO();
        BeanUtils.copyProperties(goodsClassifyVO, goodsClassifyPO);

        int row = goodsClassifyMapper.insert(goodsClassifyPO);
        if (row > 0){
            GoodsClassifyDTO goodsClassifyDTO = new GoodsClassifyDTO();
            BeanUtils.copyProperties(goodsClassifyPO, goodsClassifyDTO);

            log.info("create goods classify success end!");
            return ResultUtil.success(goodsClassifyDTO);
        }

        log.error("create goods classify fail, 商品名 {}", goodsClassifyPO.getName());

        return ResultUtil.failed(ResultCode.CREATE_GOODS_CLASSIFY_ERROR);
    }

    @Override
    public ResultUtil getGoodsClassify(Long id) {
        log.info("get goods classify begin, receive params: {}", id);

        GoodsClassifyPO goodsClassifyPO = goodsClassifyMapper.selectById(id);

        if (goodsClassifyPO != null){
            GoodsClassifyDTO goodsClassifyDTO = new GoodsClassifyDTO();
            BeanUtils.copyProperties(goodsClassifyPO, goodsClassifyDTO);

            log.info("get goods classify success end!");
            return ResultUtil.success(goodsClassifyDTO);
        }

        log.info("get goods classify empty");
        return ResultUtil.success(ResultCode.GET_GOODS_CLASSIFY_EMPTY);
    }

}
